package cn.rongdao.jrkabao.ui.information.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRImageLoad;

import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.information.necessary.NecessaryInfoBean;


/**
 * Created by jiangyongxing on 2016/7/21.
 * 描述：
 */
public class NecessaryMustInfoAdapter extends RecyclerView.Adapter<NecessaryMustInfoAdapter.NecessaryMustInfoViewHodler> {

    private List<NecessaryInfoBean.DataListEntity> list;
    private OnItemActionListener mOnItemActionListener;
    private Context mContext;

    public NecessaryMustInfoAdapter(Context context, List<NecessaryInfoBean.DataListEntity> list) {
        this.mContext = context;
        this.list = list;
    }

    @Override
    public NecessaryMustInfoViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_necessary_infomation, parent, false);
        NecessaryMustInfoViewHodler holder = new NecessaryMustInfoViewHodler(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final NecessaryMustInfoViewHodler holder, final int position) {
        String code = list.get(position).getCode();
        holder.itemMustPoint.setVisibility(list.get(position).isMust() ? View.VISIBLE : View.GONE);
        holder.itemMusttv.setVisibility(list.get(position).isMust() ? View.VISIBLE : View.GONE);
        holder.itemOtherPoint.setVisibility(!list.get(position).isMust() ? View.VISIBLE : View.GONE);
        holder.itemOthertv.setVisibility(!list.get(position).isMust() ? View.VISIBLE : View.GONE);
        holder.itemName.setText(list.get(position).getCn());
        holder.itemDesc.setText(list.get(position).getDesc());
        holder.itemStatus.setText(list.get(position).getMsg());
        FRImageLoad.loadImage(list.get(position).getLogo(), holder.itemIcon);
        holder.itemStatus.setTextColor(getStatusColor(list.get(position).getStatus()));
        holder.itemOtherTitle.setVisibility(list.get(position).isSecondFirst() ? View.VISIBLE : View.GONE);
        if (list.get(position).getJump_type() == 1) {
            holder.itemRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemActionListener != null) {
                        mOnItemActionListener.onItemClickWithUrl(list.get(position).getStatus(), list.get(position).getJump_url());
                    }
                }
            });
            return;
        }
        switch (code) {
            case "identity_info"://身份信息
                holder.itemRl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemActionListener != null) {
                            mOnItemActionListener.onIdentityInfoClick(list.get(position).getStatus(), list.get(position).getCn());
                        }
                    }
                });
                break;

            case "credit_verify"://信用卡
                holder.itemRl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemActionListener != null) {
                            mOnItemActionListener.onCreditVerifyClick(list.get(position).getStatus());
                        }
                    }
                });
                break;


            case "public_accumulation_fund"://公积金
                holder.itemRl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemActionListener != null) {
                            mOnItemActionListener.onPublicAccumulationFundClick(list.get(position).getStatus());
                        }
                    }
                });
                break;
            case "security_verify"://公积金
                holder.itemRl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemActionListener != null) {
                            mOnItemActionListener.onSecurityVerify(list.get(position).getStatus());
                        }
                    }
                });
                break;


        }
    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public void setOnItemActionListener(OnItemActionListener onItemActionListener) {
        this.mOnItemActionListener = onItemActionListener;
    }

    /**********
     * 定义点击事件
     **********/
    public interface OnItemActionListener {

        void onIdentityInfoClick(int status, String only_cn);//身份证信息

        void onCreditVerifyClick(int status);//信用卡


        void onPublicAccumulationFundClick(int status);//公积金

        void onSecurityVerify(int status);//公积金

        void onItemClickWithUrl(int status, String jump_url);
    }

    class NecessaryMustInfoViewHodler extends RecyclerView.ViewHolder {
        public TextView itemName;
        public TextView itemDesc;
        public TextView itemStatus;
        public TextView itemOtherTitle;
        public ImageView itemIcon;
        public ImageView itemMustPoint;
        public TextView itemMusttv;
        public ImageView itemOtherPoint;
        public TextView itemOthertv;
        public RelativeLayout itemRl;

        public NecessaryMustInfoViewHodler(View layout) {
            super(layout);
            itemName = (TextView) layout.findViewById(R.id.ini_name);
            itemDesc = (TextView) layout.findViewById(R.id.ini_desc);
            itemStatus = (TextView) layout.findViewById(R.id.ini_status_tv);
            itemOtherTitle = (TextView) layout.findViewById(R.id.ini_other_title);
            itemRl = (RelativeLayout) layout.findViewById(R.id.ini_rl);
            itemIcon = (ImageView) layout.findViewById(R.id.ini_imageView);
            itemMusttv = (TextView) layout.findViewById(R.id.ini_must_info_tv);
            itemMustPoint = (ImageView) layout.findViewById(R.id.ini_must_info_points);
            itemOthertv = (TextView) layout.findViewById(R.id.ini_other_info_tv);
            itemOtherPoint = (ImageView) layout.findViewById(R.id.ini_other_info_points);
        }
    }


    private int getStatusColor(int status) {
        int data = R.color.c153153153;
        switch (status) {
            case -1:
            case 1:
                data = R.color.c153153153;
                break;
            case 0:
            case 2:
                data = R.color.c515151;
                break;

        }
        return ContextCompat.getColor(mContext, data);

    }


}
